#include "input_combo.h"

InputCombo::InputCombo(QWidget *parent, const QString& name)
    : QComboBox(parent), WidgetInput(name)
{
}

void InputCombo::setItems(const QStringList& items)
{
    for (qsizetype i = 0; i < items.size(); i++)
    {
        QStringList item = items[i].split(':');
        if (item.length() == 1) this->addItem(items[i], items[i]);
        else this->addItem(item[1], item[0]);
    }
    this->setCurrentIndex(-1);
}

void InputCombo::setCallback(Callback callback)
{
    if (callback != nullptr)
    {
        connect(this, &QComboBox::currentIndexChanged, this, callback, Qt::QueuedConnection);
    }
}

void InputCombo::setType(const QString &type)
{
    _stringType = type == "string";
}

void InputCombo::setValue(const QString &value)
{
    for (int i = 0; i < this->count(); i++)
    {
        if (this->itemData(i).toString() == value)
        {
            this->setCurrentIndex(i);
            break;
        }
    }
}

QString InputCombo::getValue() const
{
    QString val = this->itemData(this->currentIndex()).toString();
    if (_stringType) return "'" + val + "'";
    return val;
}

bool InputCombo::isEmpty() const
{
    return this->currentIndex() < 0;
}
